package com.realbrand.net.realbrandpay.untils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.baidu.location.Address;
import com.baidu.mapapi.model.inner.GeoPoint;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.zxing.app.CaptureActivity;
import com.realbrand.net.realbrandpay.zxing.util.L;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import androidkun.com.versionupdatelibrary.entity.VersionUpdateConfig;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Administrator on 2017/6/22.
 */

public class Static {
    public static final String BundleKey = "key";
    public static final String BundleValue = "aaaa";
    public static final String VALUE_SCANMERCHANT = "scanMerchant";

    //drawable转成bitmap
    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;

    }

    //将bitmap通过base64转换成string
    public static String bitmapToString(Bitmap bitmap) {
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;

    }

    //将数组拼接成字符串
    public static String getString(ArrayList<String> keyList, ArrayList<String> valueList) {
        StringBuilder builder = new StringBuilder();
        builder.append("[[");
        for (int i = 0; i < keyList.size(); i++) {
            if (i < keyList.size() - 1) {
                builder.append("\"").append(keyList.get(i)).append("\"").append(",");
            } else {
                builder.append("\"").append(keyList.get(i)).append("\"");
            }
        }
        builder.append("],[");
        Log.d("zhu", "值的长度：" + valueList.size());
        for (int j = 0; j < valueList.size(); j++) {
            if (j < valueList.size() - 1) {
                builder.append("\"").append(valueList.get(j)).append("\"").append(",");
            } else {
                builder.append("\"").append(valueList.get(j)).append("\"");
            }
        }
        builder.append("]]");
        Log.d("zhu", builder.toString());
        return builder.toString();
    }

    //订单数组转字符串
    public static String string(List<List<String>> lists) {
        StringBuilder builder = new StringBuilder();
        builder.append("[[");
        for (int k = 0; k < lists.size(); k++) {
            if (k < lists.size() - 1) {
                for (int l = 0; l < lists.get(k).size(); l++) {
                    if (l < lists.get(k).size() - 1) {
                        builder.append("\"").append(lists.get(k).get(l)).append("\"").append(",");
                    } else {
                        builder.append("\"").append(lists.get(k).get(l)).append("\"");
                    }
                }
                builder.append("],[");
            } else {
                for (int l = 0; l < lists.get(k).size(); l++) {
                    if (l < lists.get(k).size() - 1) {
                        builder.append("\"").append(lists.get(k).get(l)).append("\"").append(",");
                    } else {
                        builder.append("\"").append(lists.get(k).get(l)).append("\"");
                    }
                }
            }
        }
        builder.append("]]");
        return builder.toString();
    }

    public static String stringScan(List<String> list) {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        for (int i = 0; i < list.size(); i++) {
            if (i < list.size() - 1) {
                builder.append("\"").append(list.get(i)).append("\"").append(",");
            } else {
                builder.append("\"").append(list.get(i)).append("\"");
            }
        }
        builder.append("]");
        return builder.toString();
    }

    //获取系统当前时间
    public static String getDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        return format;
    }

    public static String getMonth() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String f = df.format(new Date());
        return f;
    }

    //判断手机号是否正确
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    //判断邮箱格式是否正确
    public static boolean checkMeail(String string) {
        String regex = "[a-zA-Z0-9_]{6,12}+@[a-zA-Z]+(\\.[a-zA-Z]+){1,3}";
        return string.matches(regex);
    }

    //toast
    public static void toast(Context context, String string) {
        Toast.makeText(context, string, Toast.LENGTH_LONG).show();
    }

    //判断字符串是否同时含有数字跟字母
    public static boolean isLetterDigit(String str) {
        boolean isDigit = false;//定义一个boolean值，用来表示是否包含数字
        boolean isLetter = false;//定义一个boolean值，用来表示是否包含字母
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            } else if (Character.isLetter(str.charAt(i))) {  //用char包装类中的判断字母的方法判断每一个字符
                isLetter = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isDigit && isLetter && str.matches(regex);
        return isRight;
    }

    //存储用户信息
    public static void saveMassage(Context context, String account, String mobile, String balance, String avatar, String addresseeName,
                                   String telephone, String province, String regionCity, String countyDistrict, String detailedAddress, String code, String userType
            , String credentials, long time) {
        SharedPreferences preferences = context.getSharedPreferences("userMassage", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("userType", userType);
        editor.putString("code", code);
        editor.putString("account", account);
        editor.putString("mobile", mobile);
        editor.putString("balance", new DecimalFormat("0.00").format(Double.parseDouble(balance)));
        editor.putString("avatar", avatar);
        editor.putString("addresseeName", addresseeName);
        editor.putString("telephone", telephone);
        editor.putString("province", province);
        editor.putString("regionCity", regionCity);
        editor.putString("countyDistrict", countyDistrict);
        editor.putString("detailedAddress", detailedAddress);
        editor.putString("credentials", credentials);
        editor.putLong("time", time);
        editor.commit();
    }

    //获取当前时间戳
    public static long timeNow() {
        //获取当前时间戳
        long timeLogin = System.currentTimeMillis();
        return timeLogin;
    }

    //判断用户名字符串的长度是否是6-12位
    public static boolean stringLength(String account) {
        if (account.length() >= 6 && account.length() <= 12) {
            return true;
        } else {
            return false;
        }
    }

    //判断密码的长度
    public static boolean passwdLength(String passwd) {
        if (passwd.length() >= 6) {
            return true;
        } else {
            return false;
        }
    }

    //判断提现密码长度
    public static boolean pwLength(String pd) {
        if (pd.length() == 6) {
            return true;
        } else {
            return false;
        }
    }

    //获取APP当前版本信息
    public static int getAPPLocalVersion(Context context) {
        int currentVersionCode = 0;

        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String appVersionName = info.versionName; // 版本名
            currentVersionCode = info.versionCode; // 版本号
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return currentVersionCode;
    }

    //下载安装app
    public static void upDataAPP(String url, int code, Context context) {
        VersionUpdateConfig.getInstance()//获取配置实例
                .setContext(context)//设置上下文
                .setDownLoadURL(url)//设置文件下载链接
                .setNewVersion(code + "")//设置即将下载的APK的版本号,避免重复下载
                .setNotificationIconRes(R.drawable.logo)//设置通知图标
                .setNotificationSmallIconRes(R.drawable.logo)//设置通知小图标
                .setNotificationTitle("正品汇")//设置通知标题
                .startDownLoad();//开始下载

    }
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        //获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);  //计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight();  //统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //listView.getDividerHeight()获取子项间分隔符占用的高度
        //params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

}
